<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','image','remark','pattern','price','work_time','serve_time','times','cancel_time','isshow','paixu','createtime');
	$list=pdo_getslice('jy_meiye_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_meiye/sever/sever_detail/id='.$item['id'],true);
		}
	}else{
		$list=array(array('title'=>'洗头项目'));
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
			array('op'=>'edit','title'=>'添加项目'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			//array('name'=>'pattern','title'=>'预约服务方式','type'=>'checkbox','options'=>'pattern'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'isshow','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'是否启用','type'=>'radio','options'=>'status','width'=>'15'),
		)
	);
	$return['pattern']=array('0'=>'到店服务','1'=>'上门服务');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_meiye_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['serve_time']=$return['item']['serve_time'] ? json_decode($return['item']['serve_time'],true):array();
	$return['item']['package']=json_decode($return['item']['package']);
	$return['item']['time_box']=$return['item']['time_box']?json_decode($return['item']['time_box'],true):array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'项目名称','name'=>'title','help'=>'','ismust'=>true),
								array('type'=>'image','title'=>'项目图片','name'=>'image','help'=>'','ismust'=>true),
								//array('type'=>'checkbox','title'=>'服务方式','name'=>'pattern','help'=>'','options'=>'pattern'),
								array('type'=>'string','title'=>'项目价格','name'=>'price','help'=>'','ismust'=>true),
								array('type'=>'string','title'=>'服务时长','name'=>'times','help'=>'仅填数字，单位:分钟'),
								//array('type'=>'checkbox','title'=>'上班时间','name'=>'work_time','help'=>'','options'=>'work_time'),
								//array('type'=>'string','title'=>'可取消时间范围','name'=>'cancel_time','help'=>'仅填数字，距离服务时间在该小时之前可选择取消，单位:小时'),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'首页展示','name'=>'issh','help'=>'设置是否首页展示'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'text','title'=>'项目说明','name'=>'remark','help'=>''),
								array('type'=>'checkbox','title'=>'服务时间','name'=>'work_time','help'=>'','options'=>'work_time'),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'服务时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',
									'title2'=>'预约',
									'field1'=>'times',
									'field2'=>'yuyue',
									'field2unit'=>'人',
								),
							)
						),
						array(
							'title'=>'项目套餐设置',
							'list'=>array(
								array('type'=>'array','name'=>'package','add_tip'=>'添加一个预约套餐',
								'list'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'套餐名称'),	
									array('type'=>'string','name'=>'package_price','title'=>'服务金额'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'套餐名称','ismust'=>true),	
									array('type'=>'string','name'=>'package_price','title'=>'服务金额','ismust'=>true),
								)
								),
							)
						),
			
			)
	);
	$return['pattern']=array('0'=>'到店服务','1'=>'上门服务');
	$return['work_time']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善项目名称','error');
	}
	if(empty($postdata['image'])){
		exi('请完善项目图片','error');
	}
	if(!$postdata['price']){
		exi('请完善项目价格','error');
	}
	if($postdata['price']<0){
		exi('项目价格不规范','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'pattern'=>intval($postdata['pattern']),
		'image'=>trim($postdata['image']),
		'price'=>intval($postdata['price']),
		'work_time'=>trim($postdata['work_time']),
		'serve_time'=>ijson_encode($postdata['serve_time']),
		'package'=>ijson_encode($postdata['package']),
		'cancel_time'=>intval($postdata['cancel_time']),
		'remark'=>trim($postdata['remark']),
		'times'=>intval($postdata['times']),
		'status'=>intval($postdata['status']),
		'isshow'=>intval($postdata['issh']),
		'paixu'=>intval($postdata['paixu']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_meiye_serve',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_meiye_serve',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_meiye_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_meiye_serve',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_meiye_serve',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
